package com.huluobo.a2109mvpimdemo.addGroup;

import android.util.Log;

import com.huluobo.a2109mvpimdemo.base.BasePresenter;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMCursorResult;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupOptions;

/**
 * Created by LC on 2023/11/16.
 */
public class AddGroupPresenter extends BasePresenter<AddGroupContract.View> implements AddGroupContract.Presenter {
    @Override
    public void createGroup(String groupName, String desc, String[] allMembers, String reason, EMGroupOptions option) {
        if (isAttach()) {
            EMClient.getInstance().groupManager().asyncCreateGroup(groupName, desc, allMembers, reason, option,
                    new EMValueCallBack<EMGroup>() {
                        @Override
                        public void onSuccess(EMGroup value) {
                            getView().onCreateGroupSuccess();
                        }

                        @Override
                        public void onError(int error, String errorMsg) {
                            getView().onCreateGroupFailed(errorMsg);
                        }
                    });
        }
    }

    @Override
    public void findGroup(int pageSize, String cursor) {
        if (isAttach()) {
            EMClient.getInstance().groupManager().asyncGetPublicGroupsFromServer(pageSize, cursor,
                    new EMValueCallBack<EMCursorResult<EMGroupInfo>>() {
                        @Override
                        public void onSuccess(EMCursorResult<EMGroupInfo> value) {
                            getView().onFindGroupSuccess(value.getData());
                        }

                        @Override
                        public void onError(int error, String errorMsg) {
                            getView().onFindGroupFailed(errorMsg);
                        }
                    });
        }
    }

    @Override
    public void joinGroup(String groupId) {
        if (isAttach()) {
            EMClient.getInstance().groupManager().asyncJoinGroup(groupId, new EMCallBack() {
                @Override
                public void onSuccess() {
                    getView().onJoinGroupSuccess();
                }

                @Override
                public void onError(int code, String error) {
                    getView().onJoinGroupFailed(error,code);
                }
            });
        }
    }
}
